package com.eight.cloud.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eight.cloud.admin.entity.RoleEntity;
import com.eight.cloud.admin.facade.SysRoleFacade;
import com.eight.cloud.admin.model.dto.RoleDto;
import com.eight.cloud.admin.model.vo.RoleVo;
import com.eight.cloud.admin.service.IRoleService;
import com.eight.cloud.core.global.result.Result;
import com.eight.cloud.core.global.result.ResultCode;
import com.eight.cloud.core.utils.supply.UserContext;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色 前端控制器
 *
 * @ClassName: RoleController
 * @Author: TXC
 * @Date: 2024-10-15 15:31
 **/
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private IRoleService roleService;

    @Resource
    private SysRoleFacade sysRoleFacade;

    /**
     * 创建角色
     * @param role 角色信息
     * @return Result<RoleEntity>
     */
    @PostMapping("/add")
    public Result<RoleEntity> createRole(@RequestBody RoleDto role) {
        role.setTenantId(UserContext.getUser().getTenantId());
        roleService.createRole(role);
        return Result.ok();
    }

    @PostMapping("/edit")
    public Result<String> editRole(@RequestBody RoleDto role) {
        roleService.updateRole(role);
        return Result.ok();
    }

    /**
     * 角色权限配置
     * @param id 角色id
     * @param permissionCodes 权限编码
     */
    @PostMapping("/empower/{id}")
    public Result<String> empower(@PathVariable String id, @RequestBody List<String> permissionCodes) {
        boolean flag = roleService.empower(id, permissionCodes);
        if (flag) {
            return Result.ok();
        }
        return Result.fail(ResultCode.ERROR);
    }

    /**
     * 查询角色列表
     * @return Result<List<RoleVo>>
     */
    @GetMapping("/list")
    public Result<IPage<RoleVo>> getRoleListPage(IPage<RoleEntity> page) {
        return Result.ok(roleService.getRoleList(page));
    }

    /**
     * 删除角色
     * @param uuid 角色id
     */
    @GetMapping("/del/{uuid}")
    public Result<Boolean> delRole(@PathVariable String uuid) {
        return Result.ok(sysRoleFacade.removeRoleByUuid(uuid));
    }
}
